package com.crm.controller;

import com.crm.dto.DictDetailDto;
import com.crm.dto.DictDto;
import com.crm.entity.Dict;
import com.crm.entity.DictDetail;
import com.crm.result.ActivityResult;
import com.crm.result.PageResult;
import com.crm.result.Result;
import com.crm.service.DictDetailService;
import com.crm.service.DictService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RequestMapping("system/dict/data")
@RestController
@Slf4j
public class DictDetailController {
    @Autowired
    private DictDetailService dictDetailService;
    //分页查询字典数据表
    @GetMapping("/list")
    public ActivityResult pageDict(DictDetailDto dictDetailDto){
        PageResult res = dictDetailService.pageDict(dictDetailDto);
        return ActivityResult.success("查询成功", null, res.getRows(), res.getTotal());
    }

    //新增字典
    @PostMapping
    public Result addDict(@RequestBody DictDetailDto dictDetailDto){
        dictDetailService.add(dictDetailDto);
        return Result.success();
    }

    //删除字典
    @DeleteMapping("/{idsStr}")
    public Result deleteDict(@PathVariable String idsStr){
        List<Long> ids = Arrays.stream(idsStr.split(","))
                .map(Long::valueOf)
                .collect(Collectors.toList());
        dictDetailService.deleteBatchByIds(ids);
        return Result.success();
    }

    //根据dictCode查询字典信息
    @GetMapping("/{dictCode}")
    public Result<DictDetail> seletDictById(@PathVariable Long dictCode){
        DictDetail dictDetail = dictDetailService.selectDictById(dictCode);
        return Result.success(dictDetail);
    }

    //修改字典信息
    @PutMapping
    public Result updateDict(@RequestBody DictDetail dictDetail){
        dictDetailService.updateDict(dictDetail);
        return Result.success();
    }

    //根据字典类型查询字典详细信息
    @GetMapping("/type/{dictType}")
    public Result<List<DictDetail>> selectDictByType(@PathVariable String dictType){
        List<DictDetail> dictDetails = dictDetailService.selectDictByType(dictType);
        return Result.success(dictDetails);
    }


}
